﻿using EasyCaching.Core.Configurations;
using LJ.Abstractions.Extensions;
using LJ.Abstractions.Pack;
using LJ.Cache.EasyCaching.Internal;
using LJ.Cache.EasyCaching.Pack;
using Microsoft.Extensions.DependencyInjection;

namespace LJ.Cache.EasyCaching.Redis.Pack;

public abstract class CacheEasyCachingRedisPackBase: PackBase, ICacheEasyCachingPack
{
    public override void Add(IPackContext packContext)
    {

        packContext.ConfigManager.Add<CacheConfig>("Cache");

        packContext.DependencyManager.ServiceCollection.AddTransient<ICache, EasyCachingCache>();


        packContext.DependencyManager.ServiceCollection.AddEasyCaching((IServiceProvider a, EasyCachingOptions b) =>
        {
            var cacheConfig = a.GetConfig<CacheConfig>();

            b.UseRedis(config =>
            {
                config.DBConfig.Password = cacheConfig.Password;
                config.DBConfig.Database = cacheConfig.Database;
                config.DBConfig.Endpoints.Add(new ServerEndPoint(cacheConfig.Host, cacheConfig.Port));
            }, EasyCachingConst.Key)
            .WithMessagePack(EasyCachingConst.Key)
            ;
        });
    }
}
